import config from "../request/config";
const BASE_URL = config.servebaseURL; //公共请求头

/**
 * 日期转换
 */
export const timestampToDate = function (number, format = "Y-m-d H:i:s") {
  let date
  if (typeof (number) == 'object') {
    date = number

  } else {
    date = new Date(number)
  }
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let dates = date.getDate()
  let hours = date.getHours()
  let minutes = date.getMinutes()
  let seconds = date.getSeconds()
  let str = format
  if (str.indexOf("Y") != -1) {
    str = str.replace("Y", year)
  }
  if (str.indexOf("m") != -1) {
    str = str.replace("m", preposeZero(month))
  }
  if (str.indexOf("d") != -1) {
    str = str.replace("d", preposeZero(dates))
  }
  if (str.indexOf("H") != -1) {
    str = str.replace("H", preposeZero(hours))
  }
  if (str.indexOf("i") != -1) {
    str = str.replace("i", preposeZero(minutes))
  }
  if (str.indexOf("s") != -1) {
    str = str.replace("s", preposeZero(seconds))
  }
  return str
}
/**
 * 计算时间多久前
 */
export const friendlyFormatTime = function (stringTime) {
  let str = stringTime;
  //将字符串转换成时间格式
  let timePublish = new Date(str);
  let timeNow = new Date();
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;
  let diffValue = timeNow - timePublish;
  let diffMonth = diffValue / month;
  let diffWeek = diffValue / (7 * day);
  let diffDay = diffValue / day;
  let diffHour = diffValue / hour;
  let diffMinute = diffValue / minute;
  let diffYear = diffValue / year;
  let result = null
  if (diffValue < 0) {
    result = "刚刚";
  } else if (diffYear > 1) {
    result = parseInt(diffYear) + "年前";
  } else if (diffMonth > 1) {
    result = parseInt(diffMonth) + "月前";
  } else if (diffWeek > 1) {
    result = parseInt(diffWeek) + "周前";
  } else if (diffDay > 1) {
    result = parseInt(diffDay) + "天前";
  } else if (diffHour > 1) {
    result = parseInt(diffHour) + "小时前";
  } else if (diffMinute > 1) {
    result = parseInt(diffMinute) + "分钟前";
  } else {
    result = "刚刚";
  }
  return result;
};
// 前置补0
export const preposeZero = function (number) {
  return number < 10 ? ("0" + number) : number
}

export const imgaeUrl = function (url) {
  // export const isImg =  function(url){ // 图片地址前缀拼接
  let URL = ''
  if (url === null || url === undefined || url === '') {
    URL = ''
  } else if (typeof (url) === "string") {
    if (url.includes('http://') || url.includes('https://') || url.includes('www') || url.includes(
      'data:image')) {
      URL = url;
    } else {
      URL = BASE_URL + url;
    }
  } else {
    URL = BASE_URL + url;
  }
  return URL
};
/**
 * 支付
 *  @param {Object} data 支付的参数
 *  @param {Object}payType 支付类型
 *  @param {Object}type 小程序  app  支付  区分
 */
export const requestPay = function (data) {
  console.log(data);
  return new Promise(async (resolve, reject) => {
    wx.requestPayment({
      ...data,
      complete(ret) { return resolve(ret) },
    })
  })
}
/**
 * 页面跳转
 * 
 * @param {Object} url跳转路径
 * @param {Object} name 监听的名字
 * @param {Object} parameter 参数
 */
export const navigateTo = function (url, name, parameter) {
  wx.navigateTo({
    url,
    success: (res) => {
      res.eventChannel.emit(name, parameter)
    }
  })
}
/**
 * 返回
 */
export const navigateBack = function () {
  wx.navigateBack()
}
/**
 * @param {Object} url跳转路径
 * 关闭当前页面
 */
export const redirectTo = function (url) {
  wx.redirectTo({
    url,
  })
}
/**
 * @param {Object} url跳转路径
 * 关闭所有页面打新新的页面
 */
export const reLaunch = function (url) {
  wx.reLaunch({
    url,
  })
}





/**
 * 提示框
 */
export const toast = function (masg) {
}
/**
 * 复制  剪切板
 *  @param {Object} text 复制的 文字
 */
export const setClipboardData = function (text) {
  wx.setClipboardData({
    data: text,
    success: res => {
      console.log(res);
      wx.getClipboardData({
        success: res => {
          console.log(res)
          nativeUI('复制成功');
        }
      });
    },
  });
}
/**手机号验证 */
export const validatePhoneNumber = function (phoneNumber) {
  // 正则表达式用于验证中国大陆手机号
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phoneNumber);
}
/**轻提示 */
export const showToast = function (title, icon = 'none') {
  wx.showToast({
    title,
    icon,
  })
}
